package com.blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.common.ServerResponse;
import com.blog.pojo.MBlog;
import com.blog.pojo.MCategory;
import com.blog.service.IBlogService;
import com.blog.service.impl.BlogService;
import com.blog.vo.MBlogAndCategoryVo;
import com.blog.vo.MBlogVo;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("/blog")
public class BlogController {
	
	@Autowired
	private IBlogService service;
	

	/************ 后台 ************/
	//添加 
	@RequestMapping("/save")
	public String save(MBlog blog){
		
		service.save(blog);
		return "succ";
		
	}
	
	@RequestMapping("/findById")
	public String findById(Integer id, Model model){
		
		MBlog blog = service.selectById(id);
		model.addAttribute("blog", blog);
		return "blog/input";
	}
	
	@RequestMapping("/delete")
	public String delete(Integer id){
		
		int res =  service.delete(id);
		if(res != 0){
			return "redirect:/blog/list";
		}else return "fail";
	}
	
	
	
	
	//跳转到blog/input.jsp页面
	@RequestMapping("/input")
	public String input(){
		return "blog/input";
	}
	
	//跳转到blog/list.jsp页面
/*	@RequestMapping("/list")
	public String lists(Model model){
		
		List<MBlogVo> lists = service.selectAll();
		model.addAttribute("lists", lists);
		
		return "blog/list";
	}*/
	
	//跳转到blog/list.jsp页面
	@RequestMapping("/list")
	public String lists(Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		
		PageInfo info = service.selectAllJson(pageNum, pageSize);
		model.addAttribute("pageInfo", info);
		
		return "blog/list";
	}
	
	//跳转到blog/list.jsp页面 ，
	//返回json
	@RequestMapping("/listJson")
	@ResponseBody
	public PageInfo listJson(@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		
//		List<MBlogVo> lists = service.selectAll();
//		model.addAttribute("lists", lists);
//		return "blog/list";
		
		PageInfo info = service.selectAllJson(pageNum, pageSize);
		
		return info;
	}
	
	
	//跳转到blog/classify.jsp页面
	@RequestMapping("/classify")
	public String classify(Model model){
		
		return "blog/classify";
		
	}
	
 
	

}
